package MyApp.Model.proxy
{
	import MyApp.Model.Well;
	import MyApp.Model.WellGroup;
	
	import custom.pager.Pager;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.AbstractOperation;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	import util.Constants;
	import util.GDUtil;
	
	public class WellGroupProxy extends Proxy
	{
		public static const NAME:String=GDUtil.getUID();
		public static const DATA_LOAD_OK:String=GDUtil.getUID();
		public static const ERROR_MESSAGE_RETURN:String=GDUtil.getUID();
		public static const ADD_WG_OK:String=GDUtil.getUID();
		public static const ADD_W_OK:String=GDUtil.getUID();
		public static const LOAD_WELL_OK:String=GDUtil.getUID();
		public static const UPDATE_WG_OK:String=GDUtil.getUID();
		public function WellGroupProxy(proxyName:String=null, data:Object=null)
		{
			super(proxyName, data);
		}
		private var remote:RemoteObject;
		protected function get wellGroupService():RemoteObject{
			if(remote==null){
				remote=GDUtil.remoteObject();
				remote.destination = Constants.SER_WELL_GROUP;
			}
			return remote;
		}
		//查询所有agent
		public function loadList(pager:Pager):void{
			var tryUseAo:AbstractOperation = wellGroupService.getOperation( "findAllByPage" );  
			tryUseAo.addEventListener(ResultEvent.RESULT,listResult);
			tryUseAo.addEventListener(FaultEvent.FAULT,listFault);
			tryUseAo.send(pager);
		}
		private function listResult(e:ResultEvent):void{
			var list:ArrayCollection=e.result.data as ArrayCollection;
			this.sendNotification(DATA_LOAD_OK,{list:list,pager:e.result.pager});
		}
		private function listFault(e:FaultEvent):void{
			this.sendNotification(ERROR_MESSAGE_RETURN,{message:"数据获取失败"});
			Alert.show("数据获取失败");
		}
		//添加井组
		public function addWellGroup(wg:WellGroup):void{
			var tryUseAo:AbstractOperation = wellGroupService.getOperation( "save" );  
			tryUseAo.addEventListener(ResultEvent.RESULT,addResult);
			tryUseAo.addEventListener(FaultEvent.FAULT,addFault);
			tryUseAo.send(wg);
		}
		private function addResult(e:ResultEvent):void{
			this.sendNotification(ADD_WG_OK);
			this.loadList(new Pager());//保存成功刷新列表
		}
		private function addFault(e:FaultEvent):void{
			trace(e.message);
			this.sendNotification(ERROR_MESSAGE_RETURN,{message:"数据获取失败"});
			Alert.show("数据获取失败");
		}
		//添加井组
		public function addWell(o:Object):void{
			var w:Well=o.well;
			var pager:Pager=o.pager;
			var tryUseAo:AbstractOperation = wellGroupService.getOperation( "saveWell" );  
			var hd:Function=function (e:ResultEvent):void{
				facade.sendNotification(ADD_W_OK);
				loadList(pager);//保存成功刷新列表
				tryUseAo.removeEventListener(ResultEvent.RESULT,hd);
			};
			tryUseAo.addEventListener(ResultEvent.RESULT,hd);
			tryUseAo.addEventListener(FaultEvent.FAULT,addFault);
			tryUseAo.send(w);
		}
		public function loadWellByGroup(o:Object):void{
			var wellGroupId:int=o.wellGroupId;
			var tryUseAo:AbstractOperation = wellGroupService.getOperation( "findWellByGroup" );  
			var hd:Function=function (e:ResultEvent):void{
				facade.sendNotification(LOAD_WELL_OK,{bindList:e.result.bindList,unbindList:e.result.unbindList});
				tryUseAo.removeEventListener(ResultEvent.RESULT,hd);
			};
			tryUseAo.addEventListener(ResultEvent.RESULT,hd);
			tryUseAo.addEventListener(FaultEvent.FAULT,addFault);
			tryUseAo.send(wellGroupId);
		}
		public function updateWellGroup(wg:WellGroup):void{
			var tryUseAo:AbstractOperation = wellGroupService.getOperation( "update" );  
			var hd:Function=function (e:ResultEvent):void{
				facade.sendNotification(UPDATE_WG_OK);
				Alert.show("保存完成！");
				tryUseAo.removeEventListener(ResultEvent.RESULT,hd);
			};
			tryUseAo.addEventListener(ResultEvent.RESULT,hd);
			tryUseAo.addEventListener(FaultEvent.FAULT,addFault);
			tryUseAo.send(wg);
		}
		public function deleteWellGroup(o:Object):void{
			var wellGroupId:int=o.wellGroupId;
			var pager:Pager=o.pager;
			var tryUseAo:AbstractOperation = wellGroupService.getOperation( "doDeleteWellGroup" );  
			var hd:Function=function (e:ResultEvent):void{
				loadList(pager);//保存成功刷新列表
				tryUseAo.removeEventListener(ResultEvent.RESULT,hd);
			};
			tryUseAo.addEventListener(ResultEvent.RESULT,hd);
			tryUseAo.addEventListener(FaultEvent.FAULT,addFault);
			tryUseAo.send(wellGroupId);
		}
		public function deleteWell(o:Object):void{
			var wellGroupId:int=o.wellGroupId;
			var wellId:int=o.wellId;
			var tryUseAo:AbstractOperation = wellGroupService.getOperation( "doDeleteWell" );  
			var hd:Function=function (e:ResultEvent):void{
				facade.sendNotification(LOAD_WELL_OK,{bindList:e.result.bindList,unbindList:e.result.unbindList});
				tryUseAo.removeEventListener(ResultEvent.RESULT,hd);
			};
			tryUseAo.addEventListener(ResultEvent.RESULT,hd);
			tryUseAo.addEventListener(FaultEvent.FAULT,addFault);
			tryUseAo.send(wellId,wellGroupId);
		}
		public function bindWell(o:Object):void{
			var wellId:int=o.wellId;
			var wellGroupId:int=o.wellGroupId;
			var tryUseAo:AbstractOperation = wellGroupService.getOperation( "bind" );  
			var hd:Function=function (e:ResultEvent):void{
				facade.sendNotification(LOAD_WELL_OK,{bindList:e.result.bindList,unbindList:e.result.unbindList});
				tryUseAo.removeEventListener(ResultEvent.RESULT,hd);
			};
			tryUseAo.addEventListener(ResultEvent.RESULT,hd);
			tryUseAo.addEventListener(FaultEvent.FAULT,addFault);
			tryUseAo.send(wellId,wellGroupId);
		}
		public function unbindWell(o:Object):void{
			var wellId:int=o.wellId;
			var wellGroupId:int=o.wellGroupId;
			var tryUseAo:AbstractOperation = wellGroupService.getOperation( "unbind" );  
			var hd:Function=function (e:ResultEvent):void{
				facade.sendNotification(LOAD_WELL_OK,{bindList:e.result.bindList,unbindList:e.result.unbindList});
				tryUseAo.removeEventListener(ResultEvent.RESULT,hd);
			};
			tryUseAo.addEventListener(ResultEvent.RESULT,hd);
			tryUseAo.addEventListener(FaultEvent.FAULT,addFault);
			tryUseAo.send(wellId,wellGroupId);
		}
//		//更新agent
//		public function updateAgent(agent:Agent):void{
//			var tryUseAo:AbstractOperation = agentService.getOperation( "update" );  
//			tryUseAo.addEventListener(ResultEvent.RESULT,addResult);
//			tryUseAo.addEventListener(FaultEvent.FAULT,addFault);
//			tryUseAo.send(agent);
//		}
//		//删除agent
//		public function remove(agent:Agent):void{
//			var tryUseAo:AbstractOperation = agentService.getOperation( "remove" );  
//			tryUseAo.addEventListener(ResultEvent.RESULT,addResult);
//			tryUseAo.addEventListener(FaultEvent.FAULT,addFault);
//			tryUseAo.send(agent);
//		}
	}
}